#include "stdafx.h"
#include "oneround.h"
#include "keysbox.h"
#include "keyschedule.h"
#include "addroundkey.h"
#include "sboxlayer.h"
#include "player.h"


void OneRound( const unsigned char in_oneround[] , const unsigned char in_roundkey[] , const int round , unsigned char out_oneround[] ,unsigned char out_roundkey[] )
{
	unsigned char out_result[16] ;
	KeySchedule( in_roundkey , round , out_roundkey ) ;
	unsigned char oneroundxorkey[64] ;
	for ( int i = 0 ; i <=63 ; i++ )
	{
		oneroundxorkey[i] = in_roundkey[i+16] ;
	}
	unsigned char in_sboxstate[64] ;
	unsigned char in_playerstate[64] ;
	AddRoundKey( in_oneround , oneroundxorkey , in_sboxstate ) ;

// 	if(round == 31)
// 	{
// 		printf("sbox in value in round 31\n");
// 		for (int i = 0; i < 16; ++i)
// 		{
// 			unsigned char sbox_in = 0;
// 			for (int j = 0; j < 4; ++j)
// 			{
// 				sbox_in |= in_sboxstate[4*i+j] << j;
// 			}
// 			printf("%x ", sbox_in);
// 		}
// 		printf("\n");
// 	}
	sBoxLayer( in_sboxstate , in_playerstate ) ;
/*	faultattack_present( round , in_playerstate ) ;*/
	pLayer( in_playerstate , out_oneround ) ;
// 	for ( int i = 0 ; i <= 63 ; i++ )
// 	{
// 		printf( "%x" , out_oneround[i] ) ;
// 	}
// 	printf( "\n" ) ;
	for ( int i = 0 ; i <= 15 ; i++ )
	{
		out_result[i] = (out_oneround[4*i+3]<<3)|(out_oneround[4*i+2]<<2)|(out_oneround[4*i+1]<<1)|(out_oneround[4*i]) ;
	}
// 	for ( int i = 15 ; i >= 0 ; i-- )
// 	{
// 		printf( "%x" , out_result[i] ) ;
// 	}
// 	printf( "\n" ) ;
// 	if ( round == 31 )
// 	{
// 		for ( int i = 0 ; i <= 63 ; i++ )
// 		{
// 			printf( "%x" , out_roundkey[i] ) ;
// 			if ( (i+1)%8 == 0 )
// 			{
// 				printf( " " ) ;
// 			}
// 		}
// 	}
}